package com.zgjkhis.modules.system.rest;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zgjkhis.domain.PageResult;
import com.zgjkhis.exception.ServiceException;
import com.zgjkhis.logging.aop.log.Log;
import com.zgjkhis.modules.security.security.LoginUserUtil;
import com.zgjkhis.modules.system.domain.Message;
import com.zgjkhis.modules.system.service.MessageService;
import com.zgjkhis.modules.system.service.dto.MessageQueryCriteria;
import com.zgjkhis.modules.util.CharUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 消息管理
 *
 * @Author yzq
 */
@Api(tags = "消息管理：消息管理")
@RestController
@RequestMapping("/api/message")
public class MessageController {

    private static final String ENTITY_NAME = "message";
    private final MessageService messageService;

    public MessageController(MessageService messageService) {
        this.messageService = messageService;
    }


    @Log("查看单个消息")
    @GetMapping(value = "/detail")
    @ApiOperation("查看单个消息")
    public ResponseEntity<Object> getDetail(String id) {
        if (CharUtil.isEmpty(id)) {
            throw new ServiceException("id不能为空");
        }
        return new ResponseEntity<>(messageService.getById(id), HttpStatus.OK);
    }

    @Log("查询所有消息")
    @ApiOperation("查询所有消息")
    @GetMapping(value = "/getAllList")
    public ResponseEntity<PageResult<Message>> getAllList(MessageQueryCriteria criteria, Pageable pageable) {
        criteria.setPharmacyId(LoginUserUtil.getPharmId());
        return new ResponseEntity<>(messageService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @Log("查询所有未读消息")
    @ApiOperation("查询所有未读消息")
    @GetMapping(value = "/getAllNoList")
    public ResponseEntity<Object> getAllNoList(MessageQueryCriteria criteria) {
        criteria.setPharmacyId(LoginUserUtil.getPharmId());
        criteria.setIsLook(1);
        return new ResponseEntity<>(messageService.queryAllNo(criteria), HttpStatus.OK);
    }

    @Log("查询所有未读消息个数")
    @ApiOperation("查询所有未读消息个数")
    @GetMapping(value = "/count")
    public ResponseEntity<Object> count(MessageQueryCriteria criteria) {
        criteria.setPharmacyId(LoginUserUtil.getPharmId());
        criteria.setIsLook(1);
        return new ResponseEntity<>(messageService.count(criteria), HttpStatus.OK);
    }


    /**
     * 轮循查询消息
     */
    @Log("轮循查询消息")
    @ApiOperation(value = "轮循查询消息")
    @GetMapping("/noticeList")
    public ResponseEntity<Object> noticeList(@RequestParam("min") @ApiParam("轮循时间间隔") int min) {
        //算出当前时间的前三分钟
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar beforeTime = Calendar.getInstance();
        // 当前时间的前几分钟的时间
        beforeTime.add(Calendar.MINUTE, -min);
        Date beforeD = beforeTime.getTime();
        String time = sdf.format(beforeD);
        List<Message> messsages = messageService.list(Wrappers.<Message>query().lambda().ge(Message::getCreateTime, time).le(Message::getCreateTime, new Date()).eq(Message::getPharmacyId, LoginUserUtil.getPharmId()));
        return new ResponseEntity<>(messsages, HttpStatus.OK);
    }

    /**
     * 根据id修改已读
     */
    @Log("根据id修改已读")
    @PostMapping("/remove")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "根据id修改已读", notes = "传入ids")
    public ResponseEntity<Object> remove(@ApiParam(value = "主键集合", required = true) @RequestParam List<String> ids) {
        //批量已读
        messageService.update(Wrappers.<Message>update().lambda().set(Message::getIsLook, 2).eq(Message::getPharmacyId, LoginUserUtil.getPharmId()).in(Message::getId, ids));
        return new ResponseEntity<>(true, HttpStatus.OK);
    }

    /**
     * 删除
     */
    @Log("根据类型删除或者已读全部消息")
    @PostMapping("/removeByType")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "批量删除或已读")
    public ResponseEntity<Object> removeByType(@ApiParam("1：已读，2：删除") String type) {
        if ("1".equals(type)) {
            //批量已读
            messageService.update(Wrappers.<Message>update().lambda().set(Message::getIsLook, 2).eq(Message::getPharmacyId, LoginUserUtil.getPharmId()));
            return new ResponseEntity<>(true, HttpStatus.OK);
        } else if ("2".equals(type)) {
            //逻辑删除
            messageService.update(Wrappers.<Message>update().lambda().set(Message::getIsDelete, 1).eq(Message::getPharmacyId, LoginUserUtil.getPharmId()));
            return new ResponseEntity<>(true, HttpStatus.OK);
        } else {
            throw new ServiceException("操作类型错误");
        }
    }


}
